package com.herench.bs.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by luoxiaoming on 2017/8/9.
 */
public class DataFormatUtils {

    private static final SimpleDateFormat DATE_FORMAT_NO_SPACE = new SimpleDateFormat("yyyyMMddHHmmss");
    private static final SimpleDateFormat SIMPLE_DATE_FORMAT_NO_SPACE = new SimpleDateFormat("yyyyMMdd");
    private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat SIMPLE_DATE_FORMAT_TEMP = new SimpleDateFormat("yyyy/MM/dd");

    private static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("MM/dd");
    private static final SimpleDateFormat DAY_FORMAT_TEMP = new SimpleDateFormat("dd");
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat HOUR_MINUTE_FORMAT = new SimpleDateFormat("HH:mm");
    private static final DecimalFormat decimalFormat = new DecimalFormat("#########0.00");


    public static String getTimeStringNoSpace(Date date) {
        if (date != null) {
            return DATE_FORMAT_NO_SPACE.format(date);
        } else {
            return null;
        }
    }

    public static String getTimeString(Date date) {
        if (date != null) {
            return DATE_FORMAT.format(date);
        } else {
            return null;
        }
    }

    public static String getDayString(String date) throws ParseException {
        if (date != null) {
            return SIMPLE_DATE_FORMAT.format(DATE_FORMAT.parse(date));
        } else {
            return null;
        }
    }

    public static Date parserDay(String str) throws Exception{
        return SIMPLE_DATE_FORMAT.parse(str);
    }

    public static Date parserDayTemp(String str) throws Exception{
        return SIMPLE_DATE_FORMAT_TEMP.parse(str);
    }

    public static Date simpleParser(String str) throws Exception {
        return SIMPLE_DATE_FORMAT_NO_SPACE.parse(str);
    }

    public static Date parser(String str) throws Exception {
        return DATE_FORMAT.parse(str);
    }

    public static Date noSpaceParser(String str) throws Exception {
        return DATE_FORMAT_NO_SPACE.parse(str);
    }


    public static String getSimpleTimeString(Date date) {
        if (date != null) {
            return SIMPLE_DATE_FORMAT.format(date);
} else {
        return null;
        }
        }

    public static String getSimpleDayString(String dateStr) throws ParseException {
        if (dateStr != null) {
            Date date = SIMPLE_DATE_FORMAT.parse(dateStr);
            return DAY_FORMAT.format(date);
        } else {
            return null;
        }
    }

    public static String getSimpleDay1String(String dateStr) throws ParseException {
        if (dateStr != null) {
            Date date = SIMPLE_DATE_FORMAT.parse(dateStr);
            return DAY_FORMAT_TEMP.format(date);
        } else {
            return null;
        }
    }


    public static Date dateTimeToDate(Date dateIn) throws ParseException {
        if (dateIn != null) {
            String dateStr = SIMPLE_DATE_FORMAT.format(dateIn);
            return SIMPLE_DATE_FORMAT.parse(dateStr);
        } else {
            return null;
        }
    }
    public static String getFormatMoney(Double money){
        if (money != null) {
            String temp = decimalFormat.format(money);
            return temp;
        } else {
            return null;
        }
    }

    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;

        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static String getWeekOfDate1(Date dt) {
        String[] weekDays = {"日", "一", "二", "三", "四", "五", "六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;

        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static String transDayStrSpace(String str) throws Exception{
        Date date = SIMPLE_DATE_FORMAT_NO_SPACE.parse(str);
        return SIMPLE_DATE_FORMAT.format(date);
    }


    public static String getHourMinuteString(Date date) {
        if (date != null) {
            return HOUR_MINUTE_FORMAT.format(date);
        } else {
            return null;
        }
    }

    /**
     * 获取未来 N 天的日期
     *
     * @param Days 未来的天数
     * @return
     */
    public static String getFutureDate(int Days) {
        Calendar rightNow = Calendar.getInstance();
        //得到当前时间，+ n 天
        rightNow.add(Calendar.DAY_OF_MONTH, Days);
        //进行时间转换
        String futureDate = DATE_FORMAT.format(rightNow.getTime());

        return futureDate;
    }


    public static String now() {
        return DATE_FORMAT.format(new Date());
    }
}
